#include <stdio.h>
#include <stdlib.h>
#define SIZE 16
#define EMPTY -1

int hash(int num){
    return num % SIZE;
}

void insert(int *arr,int value){
    int index = hash(value);
    while (arr[index]!=EMPTY){
        index = (index + 1) % SIZE;
    }
    arr[index] = value;
    printf("Successfully inserted element %d into the hashtable.\n",value);
}
int search(int *arr,int value){
    int index = hash(value);
    while (arr[index]!=value){
        index = (index + 1) % SIZE;
    }
    return index;
}

void delete(int *arr,int value){
    int index = hash(value);
    while (arr[index]!=value){
        index = (index + 1) % SIZE;
    }
    arr[index] = EMPTY;
    printf("Successfully deleted %d!\n",value);
}


int main(){
    int hashtable[SIZE];
    for(int i=0;i<SIZE;i++){hashtable[i] = EMPTY;} // 初始化哈希表

    int arr[] = {218,224,391,443,22,337,1125,49,506};
    int n = sizeof(arr)/sizeof(arr[0]);
    for(int i=0;i<n;i++){
        insert(hashtable,arr[i]);
    }
    // 输出哈希表
    for(int i=1;i<SIZE;i++){printf("%d ",hashtable[i]);}
    printf("\n");

    int target = 22;
    int index = search(hashtable,target);
    printf("The index position of element %d in the hashtable is: %d\n",target,index);

    delete(hashtable,443);
    
    for(int i=1;i<SIZE;i++){printf("%d ",hashtable[i]);}
    printf("\n");

    return 0;
}